<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TCP协议标志位详解</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 30px;
        }
        
        header {
            text-align: center;
            margin-bottom: 40px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eaeaea;
        }
        
        h1 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 2.2rem;
        }
        
        .subtitle {
            color: #7f8c8d;
            font-size: 1.1rem;
        }
        
        h2 {
            color: #3498db;
            margin: 30px 0 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eaeaea;
        }
        
        h3 {
            color: #2c3e50;
            margin: 20px 0 10px;
        }
        
        .intro {
            background: #e8f4fc;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 30px;
            border-left: 4px solid #3498db;
        }
        
        .flags-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
        }
        
        .flags-table th {
            background: #3498db;
            color: white;
            padding: 15px;
            text-align: left;
        }
        
        .flags-table td {
            padding: 12px 15px;
            border-bottom: 1px solid #eaeaea;
        }
        
        .flags-table tr:nth-child(even) {
            background: #f8f9fa;
        }
        
        .flags-table tr:hover {
            background: #e8f4fc;
        }
        
        .flag-name {
            font-weight: bold;
            color: #2c3e50;
        }
        
        .flag-bit {
            background: #2c3e50;
            color: white;
            padding: 3px 8px;
            border-radius: 4px;
            font-size: 0.9rem;
            display: inline-block;
        }
        
        .connection-flow {
            display: flex;
            flex-direction: column;
            margin: 40px 0;
            position: relative;
        }
        
        .flow-step {
            display: flex;
            margin-bottom: 20px;
            align-items: center;
        }
        
        .flow-direction {
            width: 40px;
            text-align: center;
            font-weight: bold;
            color: #7f8c8d;
        }
        
        .flow-box {
            flex: 1;
            padding: 15px;
            border-radius: 8px;
            background: #f8f9fa;
            border: 1px solid #eaeaea;
            position: relative;
        }
        
        .client {
            align-self: flex-start;
            background: #e8f6f3;
            border-color: #a3e4d7;
        }
        
        .server {
            align-self: flex-end;
            background: #fdedec;
            border-color: #f5b7b1;
        }
        
        .flow-flags {
            margin-top: 8px;
            font-size: 0.9rem;
        }
        
        .flag-badge {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 4px;
            margin-right: 5px;
            font-size: 0.8rem;
            font-weight: bold;
        }
        
        .syn {
            background: #3498db;
            color: white;
        }
        
        .ack {
            background: #2ecc71;
            color: white;
        }
        
        .fin {
            background: #e74c3c;
            color: white;
        }
        
        .rst {
            background: #f39c12;
            color: white;
        }
        
        .psh {
            background: #9b59b6;
            color: white;
        }
        
        .urg {
            background: #1abc9c;
            color: white;
        }
        
        .flow-connector {
            position: absolute;
            left: 50%;
            width: 2px;
            background: #3498db;
            height: 100%;
            transform: translateX(-50%);
            z-index: -1;
        }
        
        .details {
            margin-top: 40px;
        }
        
        .flag-detail {
            margin-bottom: 25px;
            padding: 20px;
            border-radius: 8px;
            background: #f8f9fa;
            border-left: 4px solid #3498db;
        }
        
        .flag-detail h4 {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        
        .flag-detail-title {
            margin-right: 10px;
        }
        
        footer {
            margin-top: 40px;
            text-align: center;
            color: #7f8c8d;
            font-size: 0.9rem;
            padding-top: 20px;
            border-top: 1px solid #eaeaea;
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            
            h1 {
                font-size: 1.8rem;
            }
            
            .flags-table {
                font-size: 0.9rem;
            }
            
            .flags-table th, .flags-table td {
                padding: 10px;
            }
            
            .flow-box {
                padding: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>TCP协议标志位详解</h1>
            <p class="subtitle">理解TCP协议中的6个控制标志位及其在通信过程中的作用</p>
        </header>
        
        <section class="intro">
            <p>TCP（传输控制协议）是一种面向连接的、可靠的、基于字节流的传输层通信协议。在TCP报文头中，有6个控制标志位用于控制TCP连接的状态和数据传输过程。这些标志位在TCP三次握手、数据传输和连接终止过程中起着关键作用。</p>
        </section>
        
        <h2>TCP标志位概览</h2>
        <table class="flags-table">
            <thead>
                <tr>
                    <th>标志位</th>
                    <th>名称</th>
                    <th>位位置</th>
                    <th>功能描述</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="flag-name">URG</td>
                    <td>紧急指针</td>
                    <td><span class="flag-bit">第5位</span></td>
                    <td>表示报文段中有紧急数据，应尽快传送</td>
                </tr>
                <tr>
                    <td class="flag-name">ACK</td>
                    <td>确认应答</td>
                    <td><span class="flag-bit">第4位</span></td>
                    <td>表示确认号字段有效，大多数情况下此标志位都是启用的</td>
                </tr>
                <tr>
                    <td class="flag-name">PSH</td>
                    <td>推送功能</td>
                    <td><span class="flag-bit">第3位</span></td>
                    <td>接收方应尽快将数据推送给应用层，而不必等待缓冲区满</td>
                </tr>
                <tr>
                    <td class="flag-name">RST</td>
                    <td>连接重置</td>
                    <td><span class="flag-bit">第2位</span></td>
                    <td>重置由于主机崩溃或其他原因而出现错误的连接</td>
                </tr>
                <tr>
                    <td class="flag-name">SYN</td>
                    <td>同步序列号</td>
                    <td><span class="flag-bit">第1位</span></td>
                    <td>在建立连接时用于同步序列号</td>
                </tr>
                <tr>
                    <td class="flag-name">FIN</td>
                    <td>结束连接</td>
                    <td><span class="flag-bit">第0位</span></td>
                    <td>表示发送方已经没有数据要发送了，希望释放连接</td>
                </tr>
            </tbody>
        </table>
        
        <h2>TCP三次握手与标志位</h2>
        <div class="connection-flow">
            <div class="flow-connector"></div>
            
            <div class="flow-step">
                <div class="flow-direction">1</div>
                <div class="flow-box client">
                    <strong>客户端 → 服务器</strong>
                    <div class="flow-flags">
                        <span class="flag-badge syn">SYN=1</span>
                        <span class="flag-badge ack">ACK=0</span>
                    </div>
                    <p>客户端发送SYN报文，序列号Seq=X，SYN标志位设置为1</p>
                </div>
            </div>
            
            <div class="flow-step">
                <div class="flow-direction">2</div>
                <div class="flow-box server">
                    <strong>服务器 → 客户端</strong>
                    <div class="flow-flags">
                        <span class="flag-badge syn">SYN=1</span>
                        <span class="flag-badge ack">ACK=1</span>
                    </div>
                    <p>服务器响应SYN-ACK报文，序列号Seq=Y，确认号Ack=X+1，SYN和ACK标志位都设置为1</p>
                </div>
            </div>
            
            <div class="flow-step">
                <div class="flow-direction">3</div>
                <div class="flow-box client">
                    <strong>客户端 → 服务器</strong>
                    <div class="flow-flags">
                        <span class="flag-badge syn">SYN=0</span>
                        <span class="flag-badge ack">ACK=1</span>
                    </div>
                    <p>客户端发送ACK报文，序列号Seq=X+1，确认号Ack=Y+1，ACK标志位设置为1</p>
                </div>
            </div>
        </div>
        
        <h2>TCP四次挥手与标志位</h2>
        <div class="connection-flow">
            <div class="flow-connector"></div>
            
            <div class="flow-step">
                <div class="flow-direction">1</div>
                <div class="flow-box client">
                    <strong>客户端 → 服务器</strong>
                    <div class="flow-flags">
                        <span class="flag-badge fin">FIN=1</span>
                        <span class="flag-badge ack">ACK=1</span>
                    </div>
                    <p>客户端发送FIN报文，序列号Seq=U，FIN和ACK标志位设置为1</p>
                </div>
            </div>
            
            <div class="flow-step">
                <div class="flow-direction">2</div>
                <div class="flow-box server">
                    <strong>服务器 → 客户端</strong>
                    <div class="flow-flags">
                        <span class="flag-badge ack">ACK=1</span>
                    </div>
                    <p>服务器响应ACK报文，确认号Ack=U+1，ACK标志位设置为1</p>
                </div>
            </div>
            
            <div class="flow-step">
                <div class="flow-direction">3</div>
                <div class="flow-box server">
                    <strong>服务器 → 客户端</strong>
                    <div class="flow-flags">
                        <span class="flag-badge fin">FIN=1</span>
                        <span class="flag-badge ack">ACK=1</span>
                    </div>
                    <p>服务器发送FIN报文，序列号Seq=V，确认号Ack=U+1，FIN和ACK标志位设置为1</p>
                </div>
            </div>
            
            <div class="flow-step">
                <div class="flow-direction">4</div>
                <div class="flow-box client">
                    <strong>客户端 → 服务器</strong>
                    <div class="flow-flags">
                        <span class="flag-badge ack">ACK=1</span>
                    </div>
                    <p>客户端响应ACK报文，确认号Ack=V+1，ACK标志位设置为1</p>
                </div>
            </div>
        </div>
        
        <h2>标志位详细说明</h2>
        <div class="details">
            <div class="flag-detail">
                <h4>
                    <span class="flag-detail-title">SYN (Synchronize Sequence Numbers)</span>
                    <span class="flag-badge syn">SYN</span>
                </h4>
                <p>SYN标志位用于建立TCP连接时的序列号同步。当SYN=1而ACK=0时，表示这是一个连接请求报文段；当SYN=1而ACK=1时，表示这是一个同意建立连接的响应报文段。</p>
            </div>
            
            <div class="flag-detail">
                <h4>
                    <span class="flag-detail-title">ACK (Acknowledgment)</span>
                    <span class="flag-badge ack">ACK</span>
                </h4>
                <p>ACK标志位表示确认号字段有效。TCP规定，在连接建立后所有传送的报文段都必须把ACK置为1。当ACK=1时，确认号字段才有效；当ACK=0时，确认号无效。</p>
            </div>
            
            <div class="flag-detail">
                <h4>
                    <span class="flag-detail-title">FIN (Finish)</span>
                    <span class="flag-badge fin">FIN</span>
                </h4>
                <p>FIN标志位用来释放一个连接。当FIN=1时，表明此报文段的发送方的数据已经发送完毕，并要求释放连接。由于TCP连接是全双工的，因此每个方向都必须单独进行关闭。</p>
            </div>
            
            <div class="flag-detail">
                <h4>
                    <span class="flag-detail-title">RST (Reset)</span>
                    <span class="flag-badge rst">RST</span>
                </h4>
                <p>RST标志位表示连接重置。当RST=1时，表明TCP连接中出现严重差错，必须释放连接，然后再重新建立连接。RST置1还用来拒绝一个非法的报文段或拒绝打开一个连接。</p>
            </div>
            
            <div class="flag-detail">
                <h4>
                    <span class="flag-detail-title">PSH (Push)</span>
                    <span class="flag-badge psh">PSH</span>
                </h4>
                <p>PSH标志位表示推送操作。当两个应用进程进行交互式的通信时，有时在一端的应用进程希望在键入一个命令后立即就能够收到对方的响应。在这种情况下，TCP就可以使用推送操作，发送方TCP将PSH置1，并立即创建一个报文段发送出去，接收方TCP收到PSH=1的报文段，就尽快地交付给接收应用进程。</p>
            </div>
            
            <div class="flag-detail">
                <h4>
                    <span class="flag-detail-title">URG (Urgent)</span>
                    <span class="flag-badge urg">URG</span>
                </h4>
                <p>URG标志位表示紧急指针字段有效。当URG=1时，表明紧急指针字段有效，告诉系统此报文段中有紧急数据，应尽快传送，而不要按原来的排队顺序来传送。URG需要和紧急指针字段配合使用。</p>
            </div>
        </div>
        
        <footer>
            <p>TCP协议标志位详解 &copy; 2023 | 网络协议学习资料</p>
        </footer>
    </div>
</body>
</html>